package com.meitianhui.convenitenbuy.module.mine.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.AddressDefaultEventBean;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.AddressEventBean;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.util.AddressUtil;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.widget.swipemenulistview.BaseSwipListAdapter;

import java.util.List;

/**
 *  Created by amy on 2017/11/13.
 */

public class ConsumerAddressAdapter extends BaseSwipListAdapter{
    private Context mContext;
    private List<ConsumerAddress.ConsumerData> list;
    public ConsumerAddressAdapter(Context c, List<ConsumerAddress.ConsumerData> addressList){
        this.mContext = c;
        this.list = addressList;

    }
    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_mine_address,null);
            viewHolder.tvAddName= (TextView) convertView.findViewById(R.id.tv_add_name);
            viewHolder.tvAddNumber = (TextView) convertView.findViewById(R.id.tv_add_number);
            viewHolder.tvAddDefaultAddressSelect = (TextView) convertView.findViewById(R.id.tv_add_default_address);
            viewHolder.tvAddDetailAddress = (TextView) convertView.findViewById(R.id.tv_add_detail_address);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvAddName.setText(list.get(position).getConsignee());
        viewHolder.tvAddNumber.setText(list.get(position).getMobile());
        viewHolder.tvAddDetailAddress.setText(getSSQ(list.get(position).getArea_id())+list.get(position).getAddress());
        viewHolder.tvAddDefaultAddressSelect.setBackgroundResource(list.get(position).getIs_major_addr().equals("Y")?R.mipmap.mine_address_select:R.mipmap.mine_address_unselect);

        //默认地址选中RXBus
        viewHolder.tvAddDefaultAddressSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.get().post(new AddressEventBean(position));
                //通知meFragment页面进行修改
               RxBus.get().post(new AddressDefaultEventBean(getSSQ(list.get(position).getArea_id())+list.get(position).getAddress()));
            }
        });
        return convertView;
    }

    public void setData(List<ConsumerAddress.ConsumerData> consumerDatas) {
        list = consumerDatas;
        notifyDataSetChanged();
    }

    class ViewHolder{
        private TextView tvAddName,tvAddNumber,tvAddDefaultAddressSelect,tvAddDetailAddress;
    }

    private String  getSSQ(String all_id){
        String address_path="";

        if(!TextUtils.isEmpty(address_path)){
            //这里是做了数据库缓存的  可暂时忽略
            String[] d_address_path=address_path.split(",");
            if(d_address_path.length==4){
                String path1= d_address_path[1];
                String path2= d_address_path[2];
                String path3= d_address_path[3];
                if(path2.contains(path1)){
                    return path2+path3;
                }
                return path1+path2+path3;
            }else{
                return address_path;
            }
        }else{
            return AddressUtil.getRegionData(mContext,all_id);
        }


    }
    //能够左滑
    @Override
    public boolean getSwipEnableByPosition(int position) {

        return true;
    }
}
